// Copyright 2013 Bill Campbell, Swami Iyer and Bahar Akbal-Delibas

package jminusminus;

import static jminusminus.CLConstants.*;

import java.util.ArrayList;

/**
 * The AST node for a for-statement.
 * 
 * for ( [forInit] ; [expression] ; [forUpdate] ) statement
 */

class JForStatement extends JStatement {

    /** forInit statement. */
    private ArrayList<JAST> forInit;

    /** Test expression. */
    private JExpression condition;
    
    /** forUpdate statement. */
    private ArrayList<JStatement> forUpdate;
    
    /** The Body */
    private JStatement body;

    /**
     * Construct an AST node for a for-statement given its line number, the
     * forInit expression, test expression,forUpdate statement , and the body.
     * 
     * @param line
     *            line in which the while-statement occurs in the source file.
     * @param condition
     *            test expression.
     * @param body
     *            the body.
     */

    public JForStatement(int line, ArrayList<JAST> forInit, JExpression condition, ArrayList<JStatement> forUpdate, JStatement body) {
        super(line);
        this.forInit = forInit;
        this.condition = condition;
        this.forUpdate = forUpdate;
        this.body = body;
    }

    /**
     * Analysis involves analyzing the test, checking its type and analyzing the
     * body statement.
     * 
     * @param context
     *            context in which names are resolved.
     * @return the analyzed (and possibly rewritten) AST subtree.
     */

    public JForStatement analyze(Context context) {
        condition.analyze(context);
        condition.type().mustMatchExpected(line(), Type.BOOLEAN);
        body.analyze(context);
        return this;
    }

    /**
     * Generate code for the for loop.
     * 
     * @param output
     *            the code emitter (basically an abstraction for producing the
     *            .class file).
     */

    public void codegen(CLEmitter output) {
    	;
    }

    /**
     * @inheritDoc
     */

    public void writeToStdOut(PrettyPrinter p) {
        p.printf("<JForStatement line=\"%d\">\n", line());
        p.indentRight();
        if (forInit != null) {
        	p.printf("<InitialisationStatement>\n");
        	p.indentRight();
        	for (JAST e : forInit) {
        		e.writeToStdOut(p);
        	}
        	p.indentLeft();
        	p.printf("</InitialisationStatement>\n");
        }
        if (condition != null) {
        	p.printf("<TestExpression>\n");
        	p.indentRight();
        	condition.writeToStdOut(p);
        	p.indentLeft();
        	p.printf("</TestExpression>\n");
        }
        if (forUpdate != null) {
        	p.printf("<UpdateStatement>\n");
        	p.indentRight();
        	for (JStatement e : forUpdate) {
        		e.writeToStdOut(p);
        	}
        	p.indentLeft();
        	p.printf("</UpdateStatement>\n");
        }
        p.printf("<Body>\n");
        p.indentRight();
        body.writeToStdOut(p);
        p.indentLeft();
        p.printf("</Body>\n");
        p.indentLeft();
        p.printf("</JForStatement>\n");
    }

}
